﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdventureWorksWebApp
{
    public partial class listcategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loadDropDown();
        }

        private void loadDropDown()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var list2 = from cat in dc.ProductCategories
                            select cat;
                ddCategory.Items.Clear();
                foreach (ProductCategory cat in list2)
                    ddCategory.Items.Add(new ListItem(cat.Name, cat.ProductCategoryID.ToString()));

                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                ddSubcategory.Items.Clear();
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
            }
        }
        protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                ddSubcategory.Items.Clear();
                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
            }
        }

        private string generateGUID()
        {
            string guid ="";
            string chars = "1234567890";
            char[] allowedchars = chars.ToCharArray();
            Random r = new Random();
            for (int x = 0; x < 32; x++)
            {
                
                guid += allowedchars[r.Next(allowedchars.Count())];
            }
            return guid;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length > 0)
            {
                using (DataClasses1DataContext dc = new DataClasses1DataContext())
                {
                    ProductCategory cat = new ProductCategory();
                    cat.Name = TextBox1.Text;
                    cat.ModifiedDate = DateTime.Now;
                    cat.rowguid = Guid.Parse(generateGUID());

                    dc.ProductCategories.InsertOnSubmit(cat);
                    dc.SubmitChanges();
                    loadDropDown();
                }
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            if (TextBox2.Text.Length > 0)
            {
                using (DataClasses1DataContext dc = new DataClasses1DataContext())
                {
                    ProductSubcategory subcat = new ProductSubcategory();
                    subcat.Name = TextBox2.Text;
                    subcat.ModifiedDate = DateTime.Now;
                    subcat.rowguid = Guid.Parse(generateGUID());
                    subcat.ProductCategoryID = int.Parse(ddCategory.SelectedValue);

                    dc.ProductSubcategories.InsertOnSubmit(subcat);
                    dc.SubmitChanges();
                    loadDropDown();
                }
            }
        }
    }
}